##############################
#
# Replication file for the analysis in:
#
# Authoritarian multiparty governments
#
# For publication in Democratization
#
# Laure Bokobza and Jacob Nyrup
# 
##############################

####
## Set-up
####

options(scipen=99)

pacman::p_load(Hmisc,tidyverse,ggpubr,RColorBrewer,ggridges,gridExtra,huxtable,haven,lfe,mediocrethemes,
               modelsummary,readxl,survival,survminer,xtable,texreg)

# Download data

df_cross <- read_rds("df_cross_amg.rds")

df_within <- read_rds("df_within_amg.rds")

df_survival <- read_rds("df_survival.rds")

df_parties <- read_rds("df_parties.rds")

###############
## Analysis ###
###############

###
# Section 1: The rise of AMGs ---
###

### Figure 1 ---

# 1. Share of countries with more than one party in government

df_multi <- df_cross %>%
  group_by(year, country_isocode) %>%
  mutate(multiparty = ifelse(n_party > 1, 1, 
                             ifelse(is.na(n_party), NA, 0))) %>%
  group_by(year, democracy) %>%
  dplyr::summarise(mean.multi = mean(multiparty, na.rm = T, year = as.numeric(year)))

plot_multi <- ggplot(data = df_multi, aes(x = as.numeric(year), y = mean.multi), group_by(democracy)) +
  geom_line(aes(linetype = democracy,color = democracy), size = 1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Share of countries with more than one party in government") + 
  annotate("text", x = 1974, y = 0.22, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1972, y = 0.80, label = "Democracies",size=3.5) +
  xlab("") +
  scale_color_manual(values = c("#CB2314","#273046"), guide = FALSE) +
  scale_y_continuous(labels = function(x) paste0(x*100, "%"), limits = c(0,1))

# 2. Average number of parties in government

df_party <- df_cross %>%
  group_by(year,democracy) %>% dplyr::summarize(mean.parties = mean(n_party,na.rm=TRUE))

plot_parties <- ggplot(data=df_party, aes(x=year,y=mean.parties),group_by(democracy)) +
  geom_line(aes(linetype=democracy,color=democracy),size=1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Average number of parties in government") +
  annotate("text", x = 1974, y = 1.2, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1972, y = 2.7, label = "Democracies",size=3.5) +
  xlab("") +
  ylim(0,4) +
  scale_color_manual(values=c("#CB2314","#273046"),guide = FALSE)

## Save plot ##

ggsave(
  "Output/Figure1.png",
  grid.arrange(plot_multi, plot_parties),
  width = 6,
  height = 6,
  dpi = 1200
)

###
# Section 2: Multi-party government, but still autocratic ---
###

### Figure 2 ---

df_multiparty <- df_cross %>% filter(!is.na(multiparty)) %>% mutate(group = case_when(democracy == 0 & multiparty == 1 ~ "Authoritarian Multiparty Government",
                                                                                      democracy == 0 & multiparty == 0 ~ "Authoritarian aSingleparty Government",
                                                                                      democracy == 1 ~ "Democracy")) %>% filter(!is.na(group))

df_multiparty_grouped <- df_multiparty %>% group_by(group) %>% dplyr::summarize(mean_group = mean(poly,na.rm=TRUE))

plot_distpoly <- ggplot(df_multiparty, aes(poly,color = as.factor(group),fill = as.factor(group), group = as.factor(group))) +
  geom_density(
    jittered_points = FALSE, scale = 0.95, rel_min_height = 0.01,
    point_shape = "|", point_size = 3, size = 0.25,
    position = position_points_jitter(height = 0),
    alpha = 0.6,
    from = 0, to = 10
  ) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "right",plot.title = element_text(size=14),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12),
        legend.title = element_blank(),
        legend.text = element_text(size=12)) +
  geom_vline(data = df_multiparty_grouped, aes(xintercept = mean_group), size=1,color = c("#CB2314","#a1cacc","#273046"),type = 2,linetype = "longdash") + 
  xlab("Score on the Polyarchy Index") +
  ylab("Density") +
  scale_fill_manual(values = c("#a1cacc","#CB2314","#273046"), breaks = c("Authoritarian aSingleparty Government","Authoritarian Multiparty Government","Democracy"),
                    labels = c("Authoritarian single-party or no-party government","Authoritarian multiparty government","Democracy")) +
  scale_color_manual(values = c("#a1cacc","#CB2314","#273046"), , breaks = c("Authoritarian aSingleparty Government","Authoritarian Multiparty Government","Democracy"),guide = "none") +
  scale_discrete_manual("point_color", values = c("#a1cacc","#CB2314","#273046"), breaks = c("Authoritarian aSingleparty Government","Authoritarian Multiparty Government","Democracy"), guide = "none")

ggsave(
  "Output/Figure2.png",
  grid.arrange(plot_distpoly),
  width = 10,
  height = 4,
  dpi = 1200
)

# Democracies to autocracies

df_cross <- df_cross %>% group_by(country_isocode) %>% mutate(demlastfiveyears = case_when(lag(democracy,1) == 1 ~ 1,
                                                                                           lag(democracy,2) == 1 ~ 1,
                                                                                           lag(democracy,3) == 1 ~ 1,
                                                                                           lag(democracy,4) == 1 ~ 1,
                                                                                           lag(democracy,5) == 1 ~ 1,
                                                                                           TRUE ~ 0))

sharebacksliding <- df_cross %>% filter(!is.na(multiparty) & multiparty == 1 &  democracy == 0)

table(sharebacksliding$demlastfiveyears)

## Figure 4 ---

set_mediocre_all(pal = "hotcold")

surv_object = Surv(time = df_survival$duration_democratization, event = df_survival$democratization_lead1)

KM_amg = survfit(surv_object ~ multiparty, data=df_survival)

KM_survplot <- ggsurvplot(KM_amg, 
                          conf.int = TRUE,
                          conf.int.style = "step",
                          ylim = c(0.85, 1),
                          title = "Kaplan-Meier Estimate of S(t)", 
                          xlab = "Time until democratization (years)", 
                          legend = "bottom", 
                          ggtheme = theme_light(),
                          font.x = c(16),
                          font.y = c(16), 
                          font.legend = c(16),
                          font.title = c(20))

ggsave("Output/Figure4.png")

# We proceed to test whether the differences in survival probabilities are significantly different between AMGs and non AMGs:
logrank = survdiff(surv_object ~ multiparty, data = df_survival) 

###
# Section 3: Which countries adopt AMG?
###

### Figure 3 ---

mycolors <- colorRampPalette(brewer.pal(8, "RdBu"))(20)

partyshare_rank <- ggplot(data = df_parties, aes(x = reorder(country_name, -share_1), y = rank, fill = new_name)) +
  geom_bar(position = "stack", stat = "identity") +
  coord_flip() +
  labs(title = "Autocracies in 2020", x = "", y = "Party's share of cabinet positions", fill = "") +
  scale_fill_manual(values = mycolors,
                    # name = "Parties ranked by share" +
                    breaks = c("t_leaderparty","s_2","r_3","q_4","p_5","o_6","n_7","m_8","l_9","k_10","j_11","i_12","h_13","g_14","f_15","e_16","d_17","c_18","b_19","a_independent"),
                    labels = c("Leader's party","Other party #1","Other party #2", "Other party #3", "Other party #4", "Other party #5", "Other party #6",
                               "Other party #7", "Other party #8", "Other party 9", "Other party #10", "Other party #11","Other party #12","Other party #13","Other party #14","Other party #15",
                               "Other party #16","Other party #17","Other party #18","Independent")) +
  theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        axis.text.y = element_text( size = 16), 
        axis.title.x = element_text(size = 16), 
        axis.title.y = element_text(size = 16),
        title = element_text(size = 16),) +
  scale_y_continuous(labels = function(x) paste0(abs(x*100), "%"))

ggsave(
  "Output/Figure3.png",
  partyshare_rank,
  width = 11,
  height = 14,
  dpi = 120
)

### Table 1 ---

### Existence of AMGs ---

df_cross_autocracies <- df_cross %>% filter(democracy == "0" & !is.na(n_party)) 

### Regression -- AMG as DV

# Fragmentation #

lm1_exist <- felm(multiparty ~ ethbinary + year | continent | 0 | country_isocode,data=df_cross_autocracies)

# Civil war #
lm2_exist <- felm(multiparty ~ civilwarpast + year | continent | 0 | country_isocode,data=df_cross_autocracies) # cumsumcivilwargrowup # cumsumcivilwar

# Democracy #
lm3_exist <- felm(multiparty ~ poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)

# Combined #
lm_comb_exist <- felm(multiparty ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)

### Adopting AMGs ---

df_adoption <- df_cross %>% filter(adoptamg < 2 & democracy == "0" & !is.na(n_party))

# Fragmentation #

lm1_adopt <- felm(adoptamg ~ ethbinary + year | continent | 0 | country_isocode,data=df_adoption)

# Civil war #
lm2_adopt <- felm(adoptamg ~ civilwarpast + year | continent | 0 | country_isocode,data=df_adoption) # cumsumcivilwargrowup # cumsumcivilwar

# Democracy #
lm3_adopt <- felm(adoptamg ~ poly + year | continent | 0 | country_isocode,data=df_adoption)

# Combined #
lm_comb_adopt <- felm(adoptamg ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_adoption)

# Create table

texreg(l=list(lm1_exist,lm2_exist,lm3_exist, lm_comb_exist,lm1_adopt,lm2_adopt,lm3_adopt, lm_comb_adopt),
       file="Output/table1.tex",
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model","Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       custom.header = list("DV: Existence of AMG" = 1:4, "DV: Adoption of AMG" = 5:8),
       reorder.coef=c(1, 3, 4, 2),
       single.row = FALSE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       include.proj.stats = FALSE,
       label = "table:correlatesofAMG",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. Linear probability models with region fixed effects and time trends. DV in model 1-4 is existence of AMG, while DV in model 5-8 is adopting AMG (here, autocracies already with AMG are omitted). Time series from 1966-2020. Standard errors clustered by country in parentheses.}"),
       scalebox = 0.75,
       float.pos = "!htbp",
       #fontsize = "small",
       use.packages = FALSE)

###
# Section 4: Do authoritarian coalition parties have any influence?
###

# Only AMGs without real competition using V-Dem v2elmulpar_ord

amgwhereallpartiesbelongtogovernment <- df_cross %>% filter(v2elmulpar_ord == 0 & n_party > 1) %>% select(country_name,n_party,year)

### Figure 5 ---

# Type of positions ---

ind_positions <- df_within %>% filter(minister == 1 & !is.na(minister_classification) & !is.na(democracy)) %>% group_by(minister_classification,democracy) %>% dplyr::summarize(share = mean(party_group_numeric,na.rm=TRUE))

fac_order <-
  ind_positions %>%
  filter(democracy == "0") %>%
  arrange(share) %>%
  pull(minister_classification)

df_positions <-
  ind_positions %>%
  mutate(minister_classification = factor(minister_classification, levels = fac_order))

plot_positions <- ggplot(df_positions, aes(x=minister_classification, y=share, fill = democracy)) +
  geom_bar(stat="identity",position="dodge") +
  theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "right",plot.title = element_text(size=13),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12)
  ) +
  ylab("") + 
  xlab("") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%"), limits = c(0,0.35)) +
  geom_text(aes(label=paste(round(share,3)*100,"%")), position=position_dodge(width=0.9), vjust=-0.5) +
  labs(title = "Share of different types of portfolios given to coalition parties") +
  scale_fill_manual(values = c("#CB2314","#273046"), labels = c("Autocracies","Democracies")) +
  scale_color_manual(values = c("#CB2314","#273046"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#CB2314","#273046"), guide = "none") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#CB2314","#273046"),
      color = NA, point_color = NA),
    title = ""
  ))

# Importance ---

ind_prestige <- df_within %>% filter(importance_numeric %in% c(0.5,1,2,3) & !is.na(democracy)) %>% 
  group_by(importance_numeric,democracy) %>% 
  dplyr::summarize(share = mean(party_group_numeric,na.rm=TRUE)) %>%
  mutate(importance_numeric = recode(importance_numeric,
                                     `0.5` = "Junior minister\nor other low ranking post",
                                     `1` = "Low-ranking minister",
                                     `2` = "Medium-ranking minister",
                                     `3` = "Vice president, deputy prime minister,\nor top minister"
  ))

fac_order <-
  ind_prestige %>%
  filter(democracy == "0") %>%
  arrange(share) %>%
  pull(importance_numeric)

df_prestige <-
  ind_prestige %>%
  mutate(importance_numeric = factor(importance_numeric, levels = fac_order))

plot_prestige <- ggplot(df_prestige, aes(x=importance_numeric, y=share, fill = democracy)) +
  geom_bar(stat="identity",position="dodge") +
  theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "right",
        plot.title = element_text(size=13),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12)) +
  ylab("") + 
  xlab("") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(aes(label=paste(round(share,3)*100,"%")), position=position_dodge(width=0.9), vjust=-0.5) +
  labs(title = "Share of different portfolios by prestige given to coalition parties") +
  scale_fill_manual(values = c("#CB2314","#273046"), labels = c("Autocracies","Democracies")) +
  scale_color_manual(values = c("#CB2314","#273046"), guide = "none") +
  scale_discrete_manual("point_color", values = c("#CB2314","#273046"), guide = "none") +
  guides(fill = guide_legend(
    override.aes = list(
      fill = c("#CB2314","#273046"),
      color = NA, point_color = NA),
    title = ""
  ))

plot_prestige

ggsave(
  "Output/Figure5.png",
  grid.arrange(plot_positions,plot_prestige,ncol=1),
  width = 10,
  height = 8,
  dpi = 120
)

### Figure 6 ---

df_herfindahl_sum <-  df_cross %>% filter(n_party > 1 & democracy == "0" & herfindahlimportance_grouped != "No data" &  herfindahlimportance_grouped != "No power sharing" ) %>%
  group_by(year) %>%
  tally()

df_herfindahl <- df_cross %>% filter(n_party > 1 & democracy == "0" & herfindahlimportance_grouped != "No data" &  herfindahlimportance_grouped != "No power sharing" ) %>%
                group_by(year, herfindahlimportance_grouped) %>%
                dplyr::summarize(n_grouped = n()) %>% 
                ungroup() %>%
                complete(year, herfindahlimportance_grouped,
                fill = list(n_grouped = 0, freq = 0))  %>%
                left_join(.,df_herfindahl_sum,by=c("year")) %>%
                mutate(percentage = n_grouped / n,
                       herfindahlimportance_grouped = fct_relevel(herfindahlimportance_grouped,"Low power sharing","Medium power sharing","High power sharing"))

plot_herfindahl <- ggplot(df_herfindahl, aes(x=year, y=percentage, fill=herfindahlimportance_grouped)) + 
  geom_area(alpha=0.6 , size=0.1, colour="black") +
  theme_bw() +
  ylab("") +
  xlab("") +
  scale_y_continuous(labels = function(x) paste0(x*100,"%")) +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "right",
        plot.title = element_text(size=13),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12),
        legend.title = element_blank()) +
  scale_fill_brewer(palette = "RdBu", direction = 1) +
  labs(title = "AMGs by the degree of power sharing over time")

ggsave(
  "Output/Figure6.png",
  plot_herfindahl,
  width = 7,
  height = 4,
  dpi = 1200
)

##############
# Appendix ---
##############

###
# Appendix A: Description of variables and coding decisions
###

df_cross_desc <- df_cross %>% filter(democracy == "0" & !is.na(n_party)) %>% select(n_party, multiparty, share_other, herfindahl_importance, ethbinary, civilwarpast, poly)

Observations <- function(x) sum((!is.na(x)))

descstats <- modelsummary::datasummary(
  n_party + multiparty + share_other + herfindahl_importance + ethbinary + civilwarpast + poly ~ 
                         Observations + Mean*Arguments(fmt = "%.2f") + Median*Arguments(fmt = "%.2f") + SD*Arguments(na.rm=TRUE,fmt = "%.2f") + Min*Arguments(fmt = "%.2f") + Max*Arguments(fmt = "%.2f"),
                         data = df_cross_desc,
                         fmt=0,
                         title="Descriptive statistics",
                         output = 'huxtable'
)

descstats %>% .[,-1] %>% set_latex_float("htbp") %>% 
  insert_column(c("","Number of parties","AMG (binary)","Coalition parties' share of cabinet positions","Weighted Herfindahl Index",
                  "More than one active ethnic group","Civil war in the past","Polyarchy"),after = 0) %>%
  set_col_width(c(0.2, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08)) %>%
  set_top_border(row = 1,col=1:7,value=1) %>%
  set_bottom_border(row = 1,col=1:7,value=1) %>%
  set_bottom_border(row = 8,col=1:7,value=1) %>%
  set_font_size(10) %>%
  set_label("tab:descriptive") %>% 
  set_caption("Descriptive statistics") %>% 
  to_latex() %>% 
  write_lines("Output/tableA1.tex")

###
# Appendix B: Autocracies with multiple parties in the government by decade
###

year1970 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 1970 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"1970" = country_name, "Parties"= n_party)

year1980 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 1980 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"1980" = country_name, "Parties"= n_party)

year1990 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 1990 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"1990" = country_name, "Parties"= n_party)

year2000 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 2000 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"2000" = country_name, "Parties"= n_party)

year2010 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 2010 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"2010" = country_name, "Parties"= n_party)

year2020 <- df_cross %>% ungroup() %>% filter(democracy == "0" & year == 2020 & n_party > 1) %>% arrange(-n_party) %>% select(year,country_name,n_party) %>% mutate(id = row_number()) %>% select(id,"2020" = country_name, "Parties"= n_party)

combined <- full_join(year1970,year1980,by="id") %>% full_join(.,year1990,by="id") %>% full_join(.,year2000,by="id") %>% full_join(.,year2010,by="id") %>% full_join(.,year2020,by="id") %>%
  select(-1) %>% rename("Parties" = 2," Parties" = 4,"  Parties" = 6,"   Parties" = 8,"    Parties" = 10,"     Parties" = 12)

# Write

print(xtable(combined,caption = "Autocracies with more than one party in government over time",
             digits=c(0,0,0,0,0,0,0,0,0,0,0,0,0)),
      size = "tiny",
      file="Output/tableB1.tex",
)

###
# Appendix D: Repeating the analyses with alternative measure of democracy ---
###

### Plot 1 - More parties ---

# 1. Plot yearly share of regimes with more than one party in government in a given year ##

df_multi_dd <- df_cross %>%
  group_by(year, country_isocode) %>%
  mutate(multiparty = ifelse(n_party > 1, 1, 
                             ifelse(is.na(n_party), NA, 0))) %>%
  group_by(year, democracy_dd) %>%
  dplyr::summarise(mean.multi = mean(multiparty, na.rm = T, year = as.numeric(year)))

plot_multi_dd <- ggplot(data = df_multi_dd, aes(x = as.numeric(year), y = mean.multi), group_by(democracy_dd)) +
  geom_line(aes(linetype = democracy_dd,color = democracy_dd), size = 1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Share of countries with \nmore than one party in government") + 
  #  scale_y_continuous(sec.axis = sec_axis(~.*7, name = "Number of ministers"),limits=c(0.5,3.5)) +
  annotate("text", x = 1974, y = 0.22, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1972, y = 0.80, label = "Democracies",size=3.5) +
  xlab("") +
  scale_color_manual(values = c("#CB2314","#273046"), guide = FALSE) +
  scale_y_continuous(labels = function(x) paste0(x*100, "%"), limits = c(0,1))

# 2. Plot average number of parties in government by regime type (aut/dem) ##

df_party_dd <- df_cross %>%
  group_by(year,democracy_dd) %>% dplyr::summarize(mean.parties = mean(n_party,na.rm=TRUE))

plot_parties_dd <- ggplot(data=df_party_dd, aes(x=year,y=mean.parties),group_by(democracy_dd)) +
  geom_line(aes(linetype=democracy_dd,color=democracy_dd),size=1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Average number of parties in government") +
  #  scale_y_continuous(sec.axis = sec_axis(~.*7, name = "Number of ministers"),limits=c(0.5,3.5)) +
  annotate("text", x = 1974, y = 1.2, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1972, y = 2.7, label = "Democracies",size=3.5) +
  xlab("") +
  ylim(0,4) +
  scale_color_manual(values=c("#CB2314","#273046"),guide = FALSE)


## Save plot ##

ggsave(
file="../../../../../Apps/Overleaf/Parties_paper/output/figureD1.png",
  grid.arrange(plot_multi_dd, plot_parties_dd),
  width = 6,
  height = 6,
  dpi = 1200
)

### Regression

df_cross_autocracies_dd <- df_cross %>% filter(democracy_dd == "Autocracy" & !is.na(n_party)) 

### Regression -- AMG as DV

# Fragmentation #

lm1_exist_dd <- felm(multiparty ~ ethbinary + year | continent | 0 | country_isocode,data=df_cross_autocracies_dd)

# Civil war #
lm2_exist_dd <- felm(multiparty ~ civilwarpast + year | continent | 0 | country_isocode,data=df_cross_autocracies_dd)

# Democracy #
lm3_exist_dd <- felm(multiparty ~ poly + year | continent | 0 | country_isocode,data=df_cross_autocracies_dd)

# Combined #
lm_comb_exist_dd <- felm(multiparty ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_cross_autocracies_dd)

### Adopting AMGs ---

df_adoption_dd <- df_cross %>% filter(adoptamg < 2 & democracy_dd == "Autocracy" & !is.na(n_party))

# Fragmentation #

lm1_adopt_dd <- felm(adoptamg ~ ethbinary + year | continent | 0 | country_isocode,data=df_adoption_dd)

# Civil war #
lm2_adopt_dd <- felm(adoptamg ~ civilwarpast + year | continent | 0 | country_isocode,data=df_adoption_dd)

# Democracy #
lm3_adopt_dd <- felm(adoptamg ~ poly + year | continent | 0 | country_isocode,data=df_adoption_dd)

# Combined #
lm_comb_adopt_dd <- felm(adoptamg ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_adoption_dd)

# Create table

texreg(l=list(lm1_exist_dd,lm2_exist_dd,lm3_exist_dd, lm_comb_exist_dd,lm1_adopt_dd,lm2_adopt_dd,lm3_adopt_dd, lm_comb_adopt_dd),
       file="Output/tableD1.tex",
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model","Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       custom.header = list("DV: Existence of AMG" = 1:4, "DV: Adoption of AMG" = 5:8),
       reorder.coef=c(1, 3, 4, 2),
       single.row = FALSE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       include.proj.stats = FALSE,
       label = "table:correlatesofAMGdemocracy",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. Linear probability models with region fixed effects and time trends. DV in model 1-4 is existence of AMG, while DV in model 5-8 is adopting AMG (here, autocracies already with AMG are omitted). Time series from 1966-2020. Standard errors clustered by country in parentheses. We use DD-index to seperate democracies
and autocracies.}"),
       scalebox = 0.75,
       float.pos = "!htbp",
       #fontsize = "small",
       use.packages = FALSE)

###
# Appendix E: Repeating the analyses with DPI ---
###

# BjornskovRode can be downloaded at: https://sites.google.com/unav.es/martin-rode/home/data
# DPI can be downloaded at: https://datacatalog.worldbank.org/search/dataset/0039819/Database-of-Political-Institutions

na_codes2 <- function(x, ...) {
  for(y in list(...))
    x[x == y] <- NA
  x
}

df_dd <- read_excel("Data/BjornskovRode/bjornskovrode4.2.xlsx") %>% select(year, country_isocode=`country isocode`,democracy_dd=Democracy)

df_bmr <- df_cross %>% select(year,country_isocode,democracy_boix = democracy)
  
  #read.csv("../Data/BMR/whogov_boix_edited.csv") %>% select(year,country_isocode,democracy_boix)

df_dpi <- read_dta("Data/DPI2020/DPI2020.dta") %>% select(countryname,country_isocode = ifs,year,execme,gov1me,gov2me,gov3me,other = govoth) %>%
  filter(execme != "NA" & execme != "" & year < 2021) %>% # Remove country years without an executive
  mutate(across(starts_with("gov"), ~na_codes2(., "Appointees","Independents","Individual candidates",
                                               "-999","NA","No party","pro govt.","South Sudan Regional Assembly Members",
                                               "chiefs","Former Members of Sudanese Assembly","Sunni Muslim",
                                               "independent")), # Drop all text variables that do not denote a party
         other = if_else(other==-999,0,other), # Remove other without coding,
         other = if_else(other>10,10,other), # Remove other without coding
         other = replace_na(other,0),
         n_parties = rowSums(!is.na(across(starts_with("gov")))),
         n_parties = n_parties+other,
         multiparty = if_else(n_parties > 1,1,0),
         country_isocode = case_when(country_isocode == "CSK" ~ "CZE",
                                     country_isocode == "ROM" ~ "ROU",
                                     country_isocode == "YMD" ~ "YPR",
                                     country_isocode == "YSR" ~ "YUG",
                                     country_isocode == "ZAR" ~ "COD",
                                     TRUE ~ country_isocode
                                     )) %>%
  left_join(.,df_bmr,by=c("country_isocode","year")) %>%
  left_join(.,df_dd,by=c("country_isocode","year"))


# 1. Plot yearly share of regimes with more than one party in government in a given year ##

df_multi_dpi <- df_dpi %>%
  group_by(year, democracy_boix) %>%
  dplyr::summarise(mean.multi = mean(multiparty, na.rm = T, year = as.numeric(year))) %>%
  filter(!is.na(democracy_boix)) %>% mutate(democracy_boix = as.factor(democracy_boix))

plot_multi_dpi <- ggplot(data = df_multi_dpi, aes(x = as.numeric(year), y = mean.multi), group_by(democracy_boix)) +
  geom_line(aes(linetype = democracy_boix,color = democracy_boix), size = 1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Share of countries with \nmore than one party in government") + 
  #  scale_y_continuous(sec.axis = sec_axis(~.*7, name = "Number of ministers"),limits=c(0.5,3.5)) +
  annotate("text", x = 1980, y = 0.22, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1982, y = 0.60, label = "Democracies",size=3.5) +
  xlab("") +
  scale_color_manual(values = c("#CB2314","#273046"), guide = FALSE) +
  scale_y_continuous(labels = function(x) paste0(x*100, "%"), limits = c(0,1))

# 2. Plot average number of parties in government by regime type (aut/dem) ##

df_party_dpi <- df_dpi %>%
  group_by(year,democracy_boix) %>% dplyr::summarize(mean.parties = mean(n_parties,na.rm=TRUE)) %>%
  filter(!is.na(democracy_boix)) %>% mutate(democracy_boix = as.factor(democracy_boix))

plot_parties_dpi <- ggplot(data=df_party_dpi, aes(x=year,y=mean.parties),group_by(democracy_boix)) +
  geom_line(aes(linetype=democracy_boix,color=democracy_boix),size=1.15) +
  theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "none",plot.title = element_text(size=13),
        axis.title.x = element_text(size=10),
        axis.title.y = element_text(size=10)) +
  scale_x_continuous(breaks=seq(1970,2020,10)) + ylab("") +
  labs(title = "Average number of parties in government") +
  #  scale_y_continuous(sec.axis = sec_axis(~.*7, name = "Number of ministers"),limits=c(0.5,3.5)) +
  annotate("text", x = 1981, y = 1.5, label = "Autocracies",size=3.5) + 
  annotate("text", x = 1983, y = 2.7, label = "Democracies",size=3.5) +
  xlab("") +
  ylim(0,4) +
  scale_color_manual(values=c("#CB2314","#273046"),guide = FALSE)

ggsave(
  file="Output/figureE1.png",
  grid.arrange(plot_multi_dpi, plot_parties_dpi),
  width = 6,
  height = 6,
  dpi = 1200
)

# Regression table

df_dpi2 <- df_dpi %>% 
                left_join(.,select(df_cross,year,country_isocode,ethbinary,civilwarpast,poly,adoptamg,continent),by=c("country_isocode","year"))

df_dpi_cross_autocracies <- df_dpi2 %>% filter(democracy_boix == "0" & !is.na(n_parties)) 

### Regression -- AMG as DV

# Fragmentation #

lm1_exist_dpi <- felm(multiparty ~ ethbinary + year | continent | 0 | country_isocode,data=df_dpi_cross_autocracies)

# Civil war #
lm2_exist_dpi <- felm(multiparty ~ civilwarpast + year | continent | 0 | country_isocode,data=df_dpi_cross_autocracies) # cumsumcivilwargrowup # cumsumcivilwar

# Democracy #
lm3_exist_dpi <- felm(multiparty ~ poly + year | continent | 0 | country_isocode,data=df_dpi_cross_autocracies)

# Combined #
lm_comb_exist_dpi <- felm(multiparty ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_dpi_cross_autocracies)

### Adopting AMGs ---

df_dpi_adoption <- df_dpi2 %>% filter(adoptamg < 2 & democracy_boix == "0" & !is.na(n_parties))

# Fragmentation #

lm1_adopt_dpi <- felm(adoptamg ~ ethbinary + year | continent | 0 | country_isocode,data=df_dpi_adoption)

# Civil war #
lm2_adopt_dpi <- felm(adoptamg ~ civilwarpast + year | continent | 0 | country_isocode,data=df_dpi_adoption) # cumsumcivilwargrowup # cumsumcivilwar

# Democracy #
lm3_adopt_dpi <- felm(adoptamg ~ poly + year | continent | 0 | country_isocode,data=df_dpi_adoption)

# Combined #
lm_comb_adopt_dpi <- felm(adoptamg ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_dpi_adoption)

# Create table

texreg(l=list(lm1_exist_dpi,lm2_exist_dpi,lm3_exist_dpi, lm_comb_exist_dpi,lm1_adopt_dpi,lm2_adopt_dpi,lm3_adopt_dpi, lm_comb_adopt_dpi),
       file="Output/tableE1.tex",
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model","Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       custom.header = list("DV: Existence of AMG" = 1:4, "DV: Adoption of AMG" = 5:8),
       reorder.coef=c(1, 3, 4, 2),
       single.row = FALSE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs (Measure from DPI)",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       include.proj.stats = FALSE,
       label = "table:correlatesofAMGDPI",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. Linear probability models with region fixed effects and time trends. DV in model 1-4 is existence of AMG measured using DPI, while DV in model 5-8 is adopting AMG measured using DPI (here, autocracies already with AMG are omitted). Time series from 1966-2020. Standard errors clustered by country in parentheses.}"),
       scalebox = 0.75,
       float.pos = "!htbp",
       #fontsize = "small",
       use.packages = FALSE)

###
# Appendix F: Figure 3 for 1970 ---
###

df_cross_aut_1970 <- df_cross_parties %>% select(year,democracy,n_party,country_name,herfindahl_importance,leader_party,
                                                 n_1,party_1,n_2,party_2,n_3,party_3,n_4,party_4,
                                                 n_5,party_5,n_6,party_6,n_7,party_7,n_8,party_8,
                                                 n_9,party_9,n_10,party_10,n_11,party_11,n_12,party_12,
                                                 n_13,party_13,n_14,party_14,n_15,party_15,n_16,party_16,
                                                 n_17,party_17) %>%
  filter(year == 1970 & democracy == "0" & n_party >1) %>% # Uzbekistan is removed due to missing data.
  mutate(n = rowSums((.[, c("n_1","n_2", "n_3","n_4","n_5","n_6","n_7","n_8","n_9","n_10","n_11","n_12","n_13","n_14","n_15","n_16","n_17")]), na.rm = TRUE),
         share_1 = ifelse(!is.na(n_1), n_1/n, NA), 
         share_2 = ifelse(!is.na(n_2), n_2/n, NA),
         share_3 = ifelse(!is.na(n_3), n_3/n, NA),
         share_4 = ifelse(!is.na(n_4), n_4/n, NA),
         share_5 = ifelse(!is.na(n_5), n_5/n, NA),
         share_6 = ifelse(!is.na(n_6), n_6/n, NA),
         share_7 = ifelse(!is.na(n_7), n_7/n, NA),
         share_8 = ifelse(!is.na(n_8), n_8/n, NA),
         share_9 = ifelse(!is.na(n_9), n_9/n, NA),
         share_10 = ifelse(!is.na(n_10), n_10/n, NA),
         share_11 = ifelse(!is.na(n_11), n_11/n, NA),
         share_12 = ifelse(!is.na(n_12), n_12/n, NA),
         share_13 = ifelse(!is.na(n_13), n_13/n, NA),
         share_14 = ifelse(!is.na(n_14), n_14/n, NA),
         share_15 = ifelse(!is.na(n_15), n_15/n, NA),
         share_16 = ifelse(!is.na(n_16), n_16/n, NA),
         share_17 = ifelse(!is.na(n_17), n_17/n, NA))

df_cross_aut_1970_long_party <- df_cross_aut_1970 %>%
  pivot_longer(
    cols = starts_with("party_"),
    names_to = "share", 
    names_prefix = "party", 
    values_to = "name", 
    values_drop_na = TRUE
  )

df_cross_aut_1970_long_share <- df_cross_aut_1970 %>%
  pivot_longer(
    cols = starts_with("share_"),
    names_to = "share", 
    names_prefix = "share", 
    values_to = "rank", 
    values_drop_na = TRUE
  ) %>% select(rank)

df_cross_aut_1970_long <- cbind(df_cross_aut_1970_long_party,df_cross_aut_1970_long_share) %>% group_by(country_name) %>% mutate(new_name = case_when(name %in% c("leader_party","independent") ~ name,
                                                                                                                                                      share == "_1" ~ "_2",
                                                                                                                                                      share == "_2" & lag(name) != "leader_party" ~ "_3",
                                                                                                                                                      share == "_3" & lag(name,2) != "leader_party" ~ "_4",
                                                                                                                                                      share == "_4" & lag(name,3) != "leader_party" ~ "_5",
                                                                                                                                                      share == "_5" & lag(name,4) != "leader_party" ~ "_6",
                                                                                                                                                      share == "_6" & lag(name,5) != "leader_party" ~ "_7",
                                                                                                                                                      share == "_7" & lag(name,6) != "leader_party" ~ "_8",
                                                                                                                                                      share == "_8" & lag(name,7) != "leader_party" ~ "_9",
                                                                                                                                                      share == "_9" & lag(name,8) != "leader_party" ~ "_10",
                                                                                                                                                      share == "_10" & lag(name,9) != "leader_party" ~ "_11",
                                                                                                                                                      share == "_11" & lag(name,10) != "leader_party" ~ "_12",
                                                                                                                                                      TRUE ~ share),
                                                                                                                                 new_name = recode(new_name,"leader_party" = "m_leaderparty",
                                                                                                                                                   "_2" = "l_2",
                                                                                                                                                   "_3" = "k_3",
                                                                                                                                                   "_4" = "j_4",
                                                                                                                                                   "_5" = "i_5",
                                                                                                                                                   "_6" = "h_6",
                                                                                                                                                   "_7" = "g_7",
                                                                                                                                                   "_8" = "f_8",
                                                                                                                                                   "_9" = "e_9",
                                                                                                                                                   "_10" = "d_10",
                                                                                                                                                   "_11" = "c_11",
                                                                                                                                                   "_12" = "b_12",
                                                                                                                                                   "independent" = "a_independent"))

# Plot

mycolors <- colorRampPalette(brewer.pal(8, "RdBu"))(13)

partyshare_rank_1970 <- ggplot(data = df_cross_aut_1970_long, aes(x = reorder(country_name, -share_1), y = rank, fill = new_name)) +
  geom_bar(position = "stack", stat = "identity") +
  coord_flip() +
  labs(title = "Autocracies in 1970", x = "", y = "Party's share of cabinet positions", fill = "") +
  scale_fill_manual(values = mycolors,
                    # name = "Parties ranked by share" +
                    breaks = c("m_leaderparty","l_2","k_3","j_4","i_5","h_6","g_7","f_8","e_9","d_10","c_11","b_12","a_independent"),
                    labels = c("Leader's party","Other party #1","Other party #2", "Other party #3", "Other party #4", "Other party #5", "Other party #6",
                               "Other party #7", "Other party #8", "Other party 9", "Other party #10", "Other party #11","Independent")) +
  theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
        axis.text.y = element_text( size = 16), 
        axis.title.x = element_text(size = 16), 
        axis.title.y = element_text(size = 16),
        title = element_text(size = 16),) +
  scale_y_continuous(labels = function(x) paste0(abs(x*100), "%"))

partyshare_rank_1970

ggsave(
  "Output/figureF1.png",
  partyshare_rank_1970,
  width = 11,
  height = 10,
  dpi = 120
)

###
# Appendix G: Table 1 using alternative measures of AMG ---
###

### Number of parties ---

lm1_np <- felm(n_party ~ ethbinary + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm1_np)

# Civil war #
lm2_np <- felm(n_party ~ civilwarpast + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm2_np)

# Democracy #
lm3_np <- felm(n_party ~ poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm3_np)

# Combined #
lm_comb_np <- felm(n_party ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm_comb_np)

texreg(l=list(lm1_np,lm2_np,lm3_np, lm_comb_np),
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       file="Output/tableG2.tex",
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       reorder.coef=c(1, 3, 4, 2),
       single.row = TRUE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs (number of parties)",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       label = "table:correlatesofAMGnp",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. OLS with region fixed effects and time trends. Dependent variable: Number of parties. Time series from 1966-2020.
                      Standard errors clustered by country in parentheses.}"),
       scalebox = 0.9,
       float.pos = "!htbp",
       use.packages = FALSE)

### Share of cabinet positions ---

lm1_so <- felm(share_other ~ ethbinary + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm1_so)

# Civil war #
lm2_so <- felm(share_other ~ civilwarpast + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm2_so)

# Democracy #
lm3_so <- felm(share_other ~ poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm3_so)

# Combined #
lm_comb_so <- felm(share_other ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm_comb_so)

texreg(l=list(lm1_so, lm2_so, lm3_so, lm_comb_so),
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       file="Output/tableG2.tex",
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       reorder.coef=c(1, 3, 4, 2),
       single.row = TRUE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs (Coalition Parties' Share of Cabinet Positions)",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       label = "table:correlatesofAMGso",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. OLS with region fixed effects and time trends. Dependent variable: Coalition Parties' Share of Cabinet Positions.
                      Time series from 1966-2020.
                      Standard errors clustered by country in parentheses.}"),
       scalebox = 0.9,
       float.pos = "!htbp",
       use.packages = FALSE)

### Herfindahl Index ---

# Fragmentation #
lm1_hi <- felm(herfindahl_importance ~ ethbinary + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm1_hi)

# Civil war #
lm2_hi <- felm(herfindahl_importance ~ civilwarpast + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm2_hi)

# Democracy #
lm3_hi <- felm(herfindahl_importance ~ poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm3_hi)

# Combined #
lm_comb_hi <- felm(herfindahl_importance ~ ethbinary + civilwarpast + poly + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm_comb_hi)

texreg(l=list(lm1_hi,lm2_hi,lm3_hi, lm_comb_hi),
       custom.model.names = c("Ethnic Diversity","Civil War","Democratic institutions", "Multivariate model"),
       file="Output/tableG3.tex",
       custom.coef.names=c("More than one active ethnic group", "Year","Civil war in the past", "Polyarchy"),
       reorder.coef=c(1, 3, 4, 2),
       single.row = TRUE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs (Weighted Herfindahl Index)",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       label = "table:hicorrelatesofAMG",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. Linear probability models with region fixed effects and time trends. Dependent variable: Weighted Herfindahl Index.
                      Time series from 1966-2020.
                      Standard errors clustered by country in parentheses.}"),
       scalebox = 0.9,
       float.pos = "!htbp",
       use.packages = FALSE)

###
# Appendix H: Table 1 using alternative measures of the independent variables ---
###

df_cross_merged <- df_cross %>%
  mutate(yearmultiparty = ifelse(lag(n_party,1) < 2 & n_party > 1,1,0),
         dif_herfindahl = herfindahl_importance-herfindahl_minister) %>%
  group_by(country_name) %>%
  dplyr::mutate(cumsumcivil = cumsum(incidence_flag),
                fe_ethbinary = if_else(fe_plural > 0.5897, 0, 1), 
                legislature = ifelse(v2lgbicam == 0, 0, 
                                     ifelse(v2lgbicam == 1 | v2lgbicam == 2, 1, NA)),
                egip_groups_count = as.numeric(egip_groups_count))

## Existence of AMG

df_cross_autocracies <- df_cross_merged %>% filter(democracy == "0" & !is.na(n_party)) 

# Social and demographic cleavages

lm1_alt <- felm(multiparty ~ al_language2000 + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm1_alt)

lm2_alt <- felm(multiparty ~ al_ethnic2000 + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm2_alt)

lm3_alt <- felm(multiparty ~ fe_cultdiv + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm3_alt)

# Civil war

lm4_alt <- felm(multiparty ~ civilwar_growup + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm4_alt)

lm5_alt <- felm(multiparty ~ cumsumcivilwargrowup + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm5_alt)

lm6_alt <- felm(multiparty ~ civilwarpast_csp + year | continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm6_alt)

# Democracy

lm7_alt <- felm(multiparty ~ polity2 + year |  continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm7_alt)

lm8_alt <- felm(multiparty ~ v2xcs_ccsi + year |  continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm8_alt)

lm9_alt <- felm(multiparty ~ v2x_frassoc_thick + year |  continent | 0 | country_isocode,data=df_cross_autocracies)
summary(lm9_alt)

# Print regression

texreg(l=list(lm1_alt,lm2_alt,lm3_alt,lm4_alt,lm5_alt,lm6_alt,lm7_alt,lm8_alt,lm9_alt),
       custom.model.names = c("(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)","(9)"),
       file="Output/tableH1.tex",
       custom.coef.names=c("Language Fractionalization (Alesina et. al)","Year",
                           "Linguistic Fractionalization (Alesina et. al)","Cultural diversity (Fearon)",
                           "Civil war (in the given year)", "Past years with civil war","Civil war in the past (CSP)",
                           "Polity IV","Core civil society index","Electoral component index"),
       reorder.coef=c(1,3,4,5,6,7,8,9,10,2),
       single.row = FALSE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of AMGs",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       label = "table:othercorrelatesofAMG",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars.  Linear probability models with region fixed effects and time trends. 
       Dependent variable: Existence of Authoritarian Multiparty Government. Time series from 1966-2020.
       Standard errors clustered by country in parentheses.}"),
       scalebox = 0.8,
       float.pos = "!htbp",
       use.packages = FALSE)

## Adoption of AMG

df_adoption_alt <- df_cross_merged %>% filter(adoptamg < 2 & democracy == "0" & !is.na(n_party))

# Social and demographic cleavages

lm1_alt_adopt <- felm(adoptamg ~ al_language2000 + year | continent | 0 | country_isocode,data=df_adoption_alt)

lm2_alt_adopt <- felm(adoptamg ~ al_ethnic2000 + year | continent | 0 | country_isocode,data=df_adoption_alt)

lm3_alt_adopt <- felm(adoptamg ~ fe_cultdiv + year | continent | 0 | country_isocode,data=df_adoption_alt)

# Civil war

lm4_alt_adopt <- felm(adoptamg ~ civilwar_growup + year | continent | 0 | country_isocode,data=df_adoption_alt)

lm5_alt_adopt <- felm(adoptamg ~ cumsumcivilwargrowup + year | continent | 0 | country_isocode,data=df_adoption_alt)

lm6_alt_adopt <- felm(adoptamg ~ civilwarpast_csp + year | continent | 0 | country_isocode,data=df_adoption_alt)

# Democracy

lm7_alt_adopt <- felm(adoptamg ~ polity2 + year |  continent | 0 | country_isocode,data=df_adoption_alt)

lm8_alt_adopt <- felm(adoptamg ~ v2xcs_ccsi + year |  continent | 0 | country_isocode,data=df_adoption_alt)

lm9_alt_adopt <- felm(adoptamg ~ v2x_frassoc_thick + year |  continent | 0 | country_isocode,data=df_adoption_alt)

# Print regression

texreg(l=list(lm1_alt_adopt,lm2_alt_adopt,lm3_alt_adopt,lm4_alt_adopt,lm5_alt_adopt,lm6_alt_adopt,lm7_alt_adopt,lm8_alt_adopt,lm9_alt_adopt),
       custom.model.names = c("(1)","(2)","(3)","(4)","(5)","(6)","(7)","(8)","(9)"),
       file="Output/tableH2.tex",
       custom.coef.names=c("Language Fractionalization (Alesina et. al)","Year",
                           "Linguistic Fractionalization (Alesina et. al)","Cultural diversity (Fearon)",
                           "Civil war (in the given year)", "Past years with civil war","Civil war in the past (CSP)",
                           "Polity IV","Core civil society index","Electoral component index"),
       reorder.coef=c(1,3,4,5,6,7,8,9,10,2),
       single.row = FALSE,
       stars = c(0.01, 0.05,0.1),
       caption ="Correlates of adopting AMG",
       caption.above=TRUE,
       include.ci = FALSE,
       include.adjrs = FALSE,
       include.rmse = FALSE,
       include.rsquared = TRUE,
       label = "table:othercorrelatesofadoptingAMG",
       custom.gof.rows = list("Estimation method: Within region"= c("Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes")),
       include.groups = FALSE,
       custom.note = ("\\parbox{\\linewidth}{\\vspace{2pt}%stars. Linear probability models with region fixed effects and time trends. 
       Dependent variable: Adoption of Authoritarian Multiparty Government. Time series from 1966-2020.
       Standard errors clustered by country in parentheses.}"),
       scalebox = 0.8,
       float.pos = "!htbp",
       use.packages = FALSE)

